package cn.tedu.egmvc5.controller;

import cn.tedu.egmvc5.mapper.AddressMapper;
import cn.tedu.egmvc5.pojo.dto.AddressAddParam;
import cn.tedu.egmvc5.pojo.entity.Address;
import cn.tedu.egmvc5.pojo.vo.AddressInfoVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/v1/address/")
public class AddressController {
    @Autowired
    private AddressMapper addressMapper;

    @PostMapping("add")
    public String add(AddressAddParam addressAddParam) {
        System.out.println(addressAddParam);
        Address address = new Address();
        BeanUtils.copyProperties(addressAddParam, address);
        int num = addressMapper.insertAddress(address);

        return num > 0 ? "添加成功" : "添加失败";
    }

    @GetMapping("list")
    public List<AddressInfoVO> list() {
        return addressMapper.addressList();
    }

    @PostMapping("del")
    public String del(Integer id) {
        System.out.println("id:"+id);
        int num = addressMapper.deleteAddressById(id);
        return num > 0 ? "删除成功" : "删除失败";
    }

}
